<template>
  <div>
    <h1>我的理财</h1>
    <!---搜索表单-->
    <div class="search-div">
      <el-form label-width="70px" size="small">
        <el-row>
          <el-col :span="12">
            <el-form-item label="关键字">
              <el-input
                  v-model="form.fname"
                  style="width: 100%"
                  placeholder="用户名"
              ></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="创建时间">
              <el-date-picker
                  v-model="createTimes"
                  type="datetimerange"
                  range-separator="To"
                  start-placeholder="开始时间"
                  end-placeholder="结束时间"
                  format="YYYY-MM-DD HH:mm:ss"
                  value-format="YYYY-MM-DD HH:mm:ss"
              />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row style="display:flex">
          <el-button type="primary" size="small" @click="searchSysUser">
            搜索
          </el-button>
          <el-button size="small" @click="resetData">重置</el-button>
          <el-button type="success" size="small" @click="thorwdFunds">
                      已抛出的产品
          </el-button>
          <el-button type="success" size="small" @click="processFunds">
            正在理财的产品
          </el-button>
        </el-row>
      </el-form>
    </div>
            <el-table
              :data="findByUsers"
              style="width: 100%"
              row-key="id"
              border
              lazy
              :tree-props="{ children: 'children', hasChildren: 'hasChildren' }"
            >
              <el-table-column prop="fundsname" label="理财名称" />
              <el-table-column prop="remark" label="图标" #default="scope">
                <img :src="scope.row.remark" width="50" />
              </el-table-column>
              <el-table-column prop="rate" label="利率" />
              <el-table-column prop="amount" label="买入金额" />
              <el-table-column prop="createTime" label="买入时间" />
              <el-table-column prop="updateTime" label="卖出时间" />
              <el-table-column prop="redundancy1" label="理财收益" />
              <el-table-column prop="state" label="理财状态" #default="scope">
                <el-tag v-if="scope.row.state == 1" type="danger">
                  已抛出
                </el-tag>
                <el-tag v-if="scope.row.state == 0" type="success">
                  正在理财
                </el-tag>
              </el-table-column>
              <el-table-column label="操作">
                <template v-slot="scope">
                  <el-button
                    v-show="scope.row.state === 0"
                    type="primary"
                    @click="getUpdateFunds(scope.row)"
                  >
                    赎回
                  </el-button>
                </template>
              </el-table-column>
            </el-table>

    <!--分页条-->
    <el-pagination
        v-model:current-page="pageParamsList.page"
        v-model:page-size="pageParamsList.limit"
        :page-sizes="[2, 20, 50, 100]"
        @size-change="fetchSysUser"
        @current-change="fetchSysUser"
        layout="total, sizes, prev, pager, next"
        :total="total"
    />

  </div>
</template>

<script setup>

import {onMounted, reactive, ref} from "vue";
import {findByUser, getFunds, updateFunds} from "@/api/funds";
import {ElMessage, ElMessageBox} from "element-plus";

//赎回 需要用户id,产品id
const updateFundsUser = reactive({
  id: '',
  uid: '',
  fid: '',
})

const getUpdateFunds = row => {
  ElMessageBox.confirm('此操作将抛出购买的理财, 是否继续?', 'Warning', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning',
  }).then(async () => {
    updateFundsUser.id = row.id
    updateFundsUser.uid = localStorage.getItem('id')
    updateFundsUser.fid = row.fid
    const { code, message } = await updateFunds(updateFundsUser)
    if(code==200){
      ElMessage.success("操作成功")
      let item = localStorage.getItem('id')
      form.value.id=item
      fetchSysUser(pageParamsUsers.value.page,pageParamsUsers.value.limit,form.value)
    }else{
      ElMessage.error("操作失败")
    }
  })
}

//抛出查询
const thorwdFunds = () => {
  form.value.id=localStorage.getItem("id")
  form.value.goup=1
  fetchSysUser(
      pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value
  )
}
//正在理财的产品
const processFunds = () => {
  form.value.id=localStorage.getItem("id")
  form.value.goup=0
  fetchSysUser(
      pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value
  )
}

//开始和结束时间数据模型
const createTimes = ref([])

//搜索方法
const searchSysUser = () => {

  fetchSysUser(

      pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value
  )
}

const resetData = () => {
  //from清空
  form.value.fname=''
  form.value.createTimeBegin=''
  form.value.createTimeEnd=''
  form.value.id=localStorage.getItem("id")
  form.value.goup=0
  fetchSysUser(
      pageParamsUsers.value.page,
      pageParamsUsers.value.limit,
      form.value
  )
}
const fetchSysUser = async () => {
  //获取开始和结束时间
  if (createTimes.value.length === 2) {
    form.value.createTimeBegin = createTimes.value[0]
    form.value.createTimeEnd = createTimes.value[1]
  }
  let item = localStorage.getItem('id')
  form.value.id=item
  const { data } = await findByUser( pageParamsUsers.value.page,pageParamsUsers.value.limit,form.value)
  findByUsers.value = data.records
  // list.value = data.records
  total.value = data.total
}
onMounted(() => {
  fetchSysUser()
})

const queryDto = {
  fname: '',
  createTimeBegin: '',
  createTimeEnd: '',
  risk: '',
  mouthrate: '',
  yearrate: '',
  id:'',
  goup:0,
}
const form = ref(queryDto)

const findByUsers = ref([])

//分页数据
const pageParamsList = {
  page: 1,
  limit: 10,
}
const total = ref(0)
const pageParamsUsers = ref(pageParamsList)

</script>

<style scoped>

</style>
